package com.alinu.workbench.service.impl;

import com.alinu.utils.DateTimeUtil;
import com.alinu.utils.SqlSessionUtil;
import com.alinu.utils.UUIDUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.*;
import com.alinu.workbench.mapper.*;
import com.alinu.workbench.service.ClueService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/18
 * @description 线索业务实现类
 */
public class ClueServiceImpl implements ClueService {
    private final ClueMapper clueMapper = SqlSessionUtil.getSqlSession().getMapper(ClueMapper.class);
    private final ClueActivityRelationMapper carMapper = SqlSessionUtil.getSqlSession().getMapper(ClueActivityRelationMapper.class);
    private final ClueRemarkMapper clueRemarkMapper = SqlSessionUtil.getSqlSession().getMapper(ClueRemarkMapper.class);

    private final CustomerMapper customerMapper = SqlSessionUtil.getSqlSession().getMapper(CustomerMapper.class);
    private final CustomerRemarkMapper customerRemarkMapper = SqlSessionUtil.getSqlSession().getMapper(CustomerRemarkMapper.class);

    private final ContactsMapper contactsMapper = SqlSessionUtil.getSqlSession().getMapper(ContactsMapper.class);
    private final ContactsRemarkMapper contactsRemarkMapper = SqlSessionUtil.getSqlSession().getMapper(ContactsRemarkMapper.class);
    private final ContactsActivityRelationMapper conarMapper = SqlSessionUtil.getSqlSession().getMapper(ContactsActivityRelationMapper.class);


    @Override
    public boolean saveClue(Clue clue) {

        int count = clueMapper.saveClue(clue);

        return count == 1;
    }

    /**
     * 获取线索列表
     *
     * @param params 查询参数
     * @return 分页vo
     */
    @Override
    public PaginationVO<Clue> getClueList(Map<String, Object> params) {
        int total = clueMapper.getTotal(params);

        List<Clue> clueList = clueMapper.getClueList(params);

        PaginationVO<Clue> clueVO = new PaginationVO<>();

        clueVO.setDataList(clueList);
        clueVO.setTotal(total);

        return clueVO;
    }

    /**
     * 查询一个线索通过ID
     *
     * @param id 线索id
     * @return 线索对象
     */
    @Override
    public Clue findClueById(String id) {

        return clueMapper.findClueById(id);
    }

    /**
     * 新增关联市场活动
     *
     * @param clueId      线索id
     * @param activityIds 市场活动id集
     * @return 是否关联成功
     */
    @Override
    public boolean saveRelation(String clueId, String[] activityIds) {
        Map<String, Object> params = new HashMap<>();
        List<ClueActivityRelation> carList = new ArrayList<>();
        ClueActivityRelation car;
        for (String activityId : activityIds) {
            car = new ClueActivityRelation();
            car.setId(UUIDUtil.getUUID());
            car.setClueId(clueId);
            car.setActivityId(activityId);
            carList.add(car);
        }
        params.put("carList", carList);
        int count = carMapper.saveRelation(params);

        return count == activityIds.length;
    }

    /**
     * 删除线索
     *
     * @param clueId 线索id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteClue(String clueId) {

        boolean flag = true;

        // 删除线索
        int count = clueMapper.deleteClue(clueId);

        if (count != 1) {
            flag = false;
            System.out.println("删除线索失败");
        }

        // 删除线索备注
        List<ClueRemark> clueRemarkById = clueRemarkMapper.findClueRemarkById(clueId);
        int count2 = clueRemarkMapper.deleteClueRemark(clueId);

        if (count2 != clueRemarkById.size()) {
            flag = false;
            System.out.println("删除线索备注失败");
            System.out.println("应该删除备注数量为：" + clueRemarkById.size());
            System.out.println("实际删除的备注数量为：" + count2);
        }

        // 删除线索活动关联
        List<String> relatedActivityByClueId = carMapper.findRelatedActivityByClueId(clueId);

        int count3 = carMapper.deleteCAR(clueId);

        if (count3 != relatedActivityByClueId.size()) {
            flag = false;
            System.out.println("删除线索活动关联失败");
            System.out.println("应该删除的数量为：" + relatedActivityByClueId.size());
            System.out.println("实际删除的数量为：" + count3);
        }

        return flag;
    }

    @Override
    public boolean convertClue(String clueId) {
        boolean flag = true;
        Clue clue = clueMapper.findClueById(clueId);

        // 添加客户信息
        String company = clue.getCompany();
        Customer customer = customerMapper.findCompanyByName(company);
        if (customer == null) {
            customer = new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setAddress(clue.getAddress());
            customer.setCreateBy(clue.getCreateBy());
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setName(company);
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setContactSummary(clue.getContactSummary());
            customer.setDescription(clue.getDescription());
            customer.setOwner(clue.getOwner());
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());

            int count = customerMapper.saveCustomer(customer);
            if (count != 1) {
                System.out.println("添加客户信息失败");
                flag = false;
            }
        }
        // 添加联系人信息
        Contacts contacts = new Contacts();
        contacts.setId(UUIDUtil.getUUID());
        contacts.setAddress(clue.getAddress());
        contacts.setAppellation(clue.getAppellation());
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setCreateBy(clue.getCreateBy());
        contacts.setCreateTime(clue.getCreateTime());
        contacts.setDescription(clue.getDescription());
        contacts.setEmail(clue.getEmail());
        contacts.setFullname(clue.getFullname());
        contacts.setSource(clue.getSource());
        contacts.setOwner(clue.getOwner());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setCustomerId(customer.getId());

        int count = contactsMapper.saveContacts(contacts);
        if (count != 1) {
            System.out.println("添加联系人信息失败");
            flag = false;
        }
        //线索备注转换为联系人备注和客户备注
        List<ClueRemark> clueRemarkList = clueRemarkMapper.findClueRemarkById(clueId);
        for (ClueRemark clueRemark : clueRemarkList) {
            String noteContent = clueRemark.getNoteContent();

            CustomerRemark customerRemark = new CustomerRemark();
            customerRemark.setId(UUIDUtil.getUUID());
            customerRemark.setCreateBy(customer.getCreateBy());
            customerRemark.setCreateTime(customer.getCreateTime());
            customerRemark.setCustomerId(customer.getId());
            customerRemark.setEditFlag("0");
            customerRemark.setNoteContent(noteContent);

            int count2 = customerRemarkMapper.saveCustomerRemark(customerRemark);
            if (count2 != 1) {
                System.out.println("线索备注转换为客户备注失败");
                flag = false;
            }

            ContactsRemark contactsRemark = new ContactsRemark();
            contactsRemark.setContactsId(contacts.getId());
            contactsRemark.setCreateBy(contacts.getCreateBy());
            contactsRemark.setCreateTime(contacts.getCreateTime());
            contactsRemark.setEditFlag("0");
            contactsRemark.setId(UUIDUtil.getUUID());
            contactsRemark.setNoteContent(noteContent);

            int count3 = contactsRemarkMapper.saveContactsRemark(contactsRemark);
            if (count3 != 1) {
                System.out.println("线索备注转换为联系人备注失败");
                flag = false;
            }
        }

        // 将线索市场活动关联转换为联系人市场活动关联
        List<String> activityIds = carMapper.findRelatedActivityByClueId(clueId);
        for (String activityId : activityIds) {
            ContactsActivityRelation car = new ContactsActivityRelation();
            car.setId(UUIDUtil.getUUID());
            car.setActivityId(activityId);
            car.setContactsId(contacts.getId());

            int count4 = conarMapper.saveConActRelation(car);
            if (count4 != 1) {
                System.out.println("线索市场活动关联转换为联系人市场活动关联失败");
                flag = false;
            }
        }

        return flag;
    }

    /**
     * 解除关联操作
     *
     * @param carId clue_activity_relation_id
     * @return 是否解除成功
     */
    @Override
    public boolean disassocaition(String carId) {

        int count = carMapper.disassociation(carId);

        return count == 1;
    }

}
